package com.wkck.config;

import com.wkck.interceptor.JwtTokenAdminInterceptor;
import com.wkck.interceptor.JwtTokenUserInterceptor;
import com.wkck.utils.DateTimeGeneration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置拦截器
 * 配置跨域
 */

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    /**
     * 已弃用
     */
//    @Resource
//    private CookieSessionInterceptor cookieSessionInterceptor;

    @Resource
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 配置跨域Cors
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /**
         * 所有请求都允许跨域
         */
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowedMethods("POST","GET","DELETE","UPDATE","OPTIONS")
                .exposedHeaders("*")
                .maxAge(10 * 1000);
    }

    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket1() {
        log.info("生成管理端接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("校园兼职管理系统")
                .version("2.0")
                .description("校园兼职管理系统接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wkck.controller.admin"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean
    public Docket docket2() {
        log.info("生成用户端接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("校园兼职管理系统")
                .version("2.0")
                .description("校园兼职管理系统接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wkck.controller.frontend"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 设置静态资源映射
     * @param registry
     */


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册拦截器");

        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/manage/**")
                .addPathPatterns("/admin/**")
                .addPathPatterns("/career/**")
                .addPathPatterns("/company/**")
                .addPathPatterns("/common/**")
                .addPathPatterns("/newsBg/**")
                .addPathPatterns("/noticeBg/**")
                .addPathPatterns("/platformResumeBg/**")
                .addPathPatterns("/fileResumeBg/**")
                .addPathPatterns("/deliverBg/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/fileResumeBg/file/online/**");
        log.info("管理端拦截器已注册 =======> {}", DateTimeGeneration.now());

        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/user/login")
                .excludePathPatterns("/user/frontend/**")
                .excludePathPatterns("/user/career/list")
                .excludePathPatterns("/user/career/get/**")
                .excludePathPatterns("/user/career/list/name")
                .excludePathPatterns("/user/company/list")
                .excludePathPatterns("/user/company/name")
                .excludePathPatterns("/user/resume/file/online/**")
                .excludePathPatterns("/user/news/**");

        log.info("用户端拦截器已注册 =======> {}", DateTimeGeneration.now());

    }
}
